package cn.edu.nwpu.warehousemanagement.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * 消息队列配置
 *
 * @author Loadstar
 * @version 1.0
 */
@Configuration
public class RabittMQConfig {

    @Value("${custom.rabbitmq.predict-queue}")
    private String predictQueue;

    @Value("${custom.rabbitmq.result-queue}")
    private String resultQueue;

    @Value("${custom.rabbitmq.dead-letter-queue}")
    private String deadLetterQueue;

    @Value("${custom.rabbitmq.dead-letter-exchange}")
    private String deadLetterExchange;

    @Value("${custom.rabbitmq.dead-letter-routing-key}")
    private String deadLetterRoutingKey;

    @Value("${custom.rabbitmq.max-deliveries}")
    private int maxDeliveries;

    @Value("${custom.rabbitmq.message-ttl}")
    private int messageTtl;

    @Bean
    public Queue predictQueue() {
        return QueueBuilder.durable(predictQueue)
                .withArgument("x-dead-letter-exchange", deadLetterExchange)
                .withArgument("x-dead-letter-routing-key", deadLetterRoutingKey)
                .withArgument("x-max-deliveries", maxDeliveries)
                .withArgument("x-message-ttl", messageTtl)
                .build();
    }

    // 结果队列
    @Bean
    public Queue resultQueue() {
        return new Queue(resultQueue, true);
    }

    // 新增死信交换机定义
    @Bean
    public DirectExchange deadLetterExchange() {
        return new DirectExchange(deadLetterExchange, true, false);
    }

    // 显式声明死信队列（已在配置参数中存在）
    @Bean
    public Queue deadLetterQueue() {
        return new Queue(deadLetterQueue, true, false, false);
    }

    // 新增绑定关系配置
    @Bean
    public Binding bindDeadLetter() {
        return BindingBuilder.bind(deadLetterQueue())
                .to(deadLetterExchange())
                .with(deadLetterRoutingKey);
    }
}
